package com.ruoyi.forum.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.forum.domain.ForumPost;
import com.ruoyi.forum.domain.ForumStudent;
import com.ruoyi.forum.domain.vo.CollectUpvote;
import com.ruoyi.forum.service.IForumPostService;
import com.ruoyi.forum.utils.SocialistCoreCheck;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 论坛-贴子Controller
 * 
 * @author ruoyi
 * @date 2024-02-11
 */
@RestController
@RequestMapping("/forum/post")
public class ForumPostController extends BaseController
{
    @Autowired
    private IForumPostService forumPostService;

    @Autowired
    private SocialistCoreCheck socialistCoreCheck;

    /**
     * 查询论坛-贴子列表
     * @RequestParam("schoolId")@RequestParam("collegeId")@RequestParam("specialtyId")
     * queryType 查询类型（1：收藏，2：点赞）
     */
    @PreAuthorize("@ss.hasPermi('forum:post:list')")
    @GetMapping("/list")
    public TableDataInfo list(ForumPost forumPost ,  Long schoolId,
                               Long collegeId,
                               Long specialtyId, Integer queryType, Long queryId)
    {
        startPage();
        startOrderBy();
        //不查询已经假删除的数据
        if(forumPost.getDelFlag()==null){
            forumPost.setDelFlag("0");
        }
        //添加查询选项收藏，点赞
        if(queryType!=null){
            if(forumPost.getCollectUpvote()==null){
                forumPost.setCollectUpvote(new CollectUpvote());
            }
            forumPost.getCollectUpvote().setQueryType(queryType);
            forumPost.getCollectUpvote().setUserId(queryId);
        }
        //添加查询选项学校学院专业
        if(schoolId!=null){
            if(forumPost.getForumStudent()==null){
                forumPost.setForumStudent(new ForumStudent());
            }
            forumPost.getForumStudent().setSchoolId(schoolId);
        }
        if(collegeId!=null){
            if(forumPost.getForumStudent()==null){
                forumPost.setForumStudent(new ForumStudent());
            }
            forumPost.getForumStudent().setCollegeId(collegeId);
        }
        if(specialtyId!=null){
            if(forumPost.getForumStudent()==null){
                forumPost.setForumStudent(new ForumStudent());
            }
            forumPost.getForumStudent().setSpecialtyId(specialtyId);
        }
        List<ForumPost> list = forumPostService.selectForumPostList(forumPost);
        return getDataTable(list);
    }

    /**
     * 导出论坛-贴子列表
     */
    @PreAuthorize("@ss.hasPermi('forum:post:export')")
    @Log(title = "论坛-贴子", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ForumPost forumPost)
    {
        List<ForumPost> list = forumPostService.selectForumPostList(forumPost);
        ExcelUtil<ForumPost> util = new ExcelUtil<ForumPost>(ForumPost.class);
        util.exportExcel(response, list, "论坛-贴子数据");
    }

    /**
     * 获取论坛-贴子详细信息
     */
    @PreAuthorize("@ss.hasPermi('forum:post:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        ForumPost forumPost = forumPostService.selectForumPostById(id);
        return forumPost==null?new AjaxResult(404,"资源，服务未找到"):success(forumPost);
    }

    /**
     * 新增论坛-贴子
     */
    @PreAuthorize("@ss.hasPermi('forum:post:add')")
    @Log(title = "论坛-贴子", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ForumPost forumPost)
    {
        boolean check = socialistCoreCheck.checkContent(forumPost.getContent());
        if (!check){
            return new AjaxResult(HttpStatus.check,"发布内容有问题，请重新编辑更改，要符合社会主义核心价值观！");
        }
        forumPost.setCreateBy(getUsername());
        return toAjax(forumPostService.insertForumPost(forumPost));
    }

    /**
     * 修改论坛-贴子
     */
    @PreAuthorize("@ss.hasPermi('forum:post:edit')")
    @Log(title = "论坛-贴子", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ForumPost forumPost)
    {
        forumPost.setUpdateBy(getUsername());
        return toAjax(forumPostService.updateForumPost(forumPost));
    }

    /**
     * 删除论坛-贴子
     */
    @PreAuthorize("@ss.hasPermi('forum:post:remove')")
    @Log(title = "论坛-贴子", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(forumPostService.deleteForumPostByIds(ids));
    }

    /**
     * 查询某一天的贴子发布量。
     */
//    @Log(title = "论坛-贴子", businessType = BusinessType.DELETE)
    @GetMapping("/number/{date}")
    @Anonymous
    public Map<String, Object> getPostNumberForDay(@PathVariable Date date)
    {
        int i = forumPostService.selectForumPostByDate(date);
        HashMap<String,Object> map = new HashMap<>();
        map.put("code",HttpStatus.SUCCESS);
        map.put("msg","查询成功");
        HashMap<String , Object> data = new HashMap<>();
        data.put("date",date);
        data.put("size",i);
        map.put("data", data);
        return map;
    }
}
